package com.itheima.service.impl;

import com.itheima.dao.GoodsDao;
import com.itheima.pojo.Goods;
import com.itheima.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangyan
 * @date 2021/2/24 15:23
 */

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsDao goodsDao;

    /**
     * {
     * total:1000    总条数
     * goodsList:[]  当前页展示的数据
     * }
     */
    @Override
    public Map<String, Object> findByPage(Map<String, Object> map) {
        //1. 查询总条数
        long total = goodsDao.getTotalCount();

        //2. 查询当前页展示数据(分页查询) limit
        //当前页码
        Integer currentPage = (Integer) map.get("currentPage");
        //每页条数
        Integer pageSize = (Integer) map.get("pageSize");
        //计算 起始索引 (currentPage-1)*pageSize
        Integer startIndex = (currentPage - 1) * pageSize;

        //查询数据
        List<Goods> goodsList = goodsDao.findPage(startIndex,pageSize);

        //创建返回值
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("total",total);
        resultMap.put("goodsList",goodsList);

        return resultMap;
    }

    @Override
    public void add(Goods goods) {
        goodsDao.add(goods);
    }

    @Override
    public Goods findById(Integer id) {
        return goodsDao.findById(id);
    }

    @Override
    public void edit(Goods goods) {
        goodsDao.edit(goods);
    }

    @Override
    public void delById(Integer id) {
        goodsDao.delById(id);
    }

}
